--
-- @Author:      feilong
-- @DateTime:    2019-08-19 14:02:03
-- @Description: 用户对象处理

local table_insert = table.insert
local table_remove = table.remove
local math_floor = math.floor

local UserInfo = class("UserInfo")


--构造函数
function UserInfo:ctor(base_data, enter_data)

    --角色数据
    self.uid = base_data.uid
    self.coins = base_data.coins --身上金币
    self.vip_level = base_data.vip_level or 0 --vip等级
    self.safe_box = base_data.safe_box or 0 --保险箱

    self.name = enter_data.name --用户名        
    self.icon = enter_data.icon --头像
    self.sex = enter_data.sex --性别
    self.icon_border = enter_data.icon_border --头像框
    self.os_system = enter_data.os_system --手机系统
    self.platform = enter_data.platform --平台

    self.total_bet = 0 --入桌后下注金额
    self.play_count = 0 --玩牌局数
    self.win_count = 0 --获胜局数
    self.lately20 = {} --最近20局输赢
    self.speedup_values = base_data.speedup_values or {} --玩家提速值
    self.affect_count = base_data.affect_count
    self.affect_rate = base_data.affect_rate
    self.init_coins = self.coins --游戏开始下注时所拥有的金币值
    self.qhb_record = {} --抢红包游戏记录
    self.fhb_record = {} --发红包游戏记录
    self.enter_coins = 0 --进入桌子时身上的金币
    self.enter_safe_coins = 0 --进入桌子时存款金币
    self.enter_time = 0 --进入桌子时的时间
    self.win_lose = 0 --入桌后的总输赢(不算台费)
    self.free = 0 --入桌后的总服务费
    self.produce = 0 --入桌后的总产出
    self.cur_bet = 0 --当前局投注


    --进入桌子时获取的属性
    self.fd = nil --sokect id
    self.client_version = nil
    self.channel = nil --渠道
    self.cur_channel = nil --当前渠道
    self.device_id = nil
    self.device_brand = nil
    self.is_robot = false --是否是机器人
    self.robot_type = nil --机器人类型
    self.robot_type_id = nil --机器人类型id (对应的数字在robot.lua配置中)

    --游戏中数据-------------------------------------------------------------------------------
    self.seat_id = 0
    self.desk_id = global.room_config:getTableId() --桌子id
    self.can_bet_lit = nil --可下注列表
    
    self.last_chat_time = 0 --最后发送聊天的时间
    self.playing = false --游戏状态中
    self.disconnect = false --离线状态中
    self.beted = false --已下注状态中

    -------------百家乐
    self.rate_type = 0 --赔率类型 0=传统，1=免佣
    self.no_bet_count = 0 --连续没下注的次数 （超过3次会被踢出桌子

end

--创建时从外部传入的配置
--enterReq -> data
function UserInfo:init(data)
    self.fd = data.fd --sokect id
    self.client_version = data.client_version
    self.channel = data.channel
    self.cur_channel = data.cur_channel
    self.device_id = data.device_id
    self.device_brand = data.device_brand

    self:setEnterCoins()
    self:setEnterSafeCoins()
    self:setEnterTime()
    self:setWinLose() --进入桌子后的输赢累计
    self:setFree()  
end

function UserInfo:getId()
    return self.uid
end

--取user id
function UserInfo:getUid()
    return self.uid
end
--取座位id
function UserInfo:getSeatId()
    return self.seat_id
end
function UserInfo:setSeatId(seat_id)
    self.seat_id = seat_id
end
--是否有座位
function UserInfo:isSeated()
    return self.seat_id>0
end

--取桌子id
function UserInfo:getDeskId()
    return self.desk_id
end

function UserInfo:getPosition()
    return self.seat_id
end
function UserInfo:getName()
    return self.name
end
function UserInfo:getCoins()
    return self.coins
end
function UserInfo:setCoins(coins)
    if not coins then 
        return 
    end
    self.coins = coins
end
function UserInfo:addCoins(coins)
    self.coins = self.coins + coins
end
function UserInfo:getInitCoins()
    return self.init_coins
end
function UserInfo:reSetInitCoins()
    self.init_coins = self.coins
end
function UserInfo:setInitCoins(init_coins)
    self.init_coins = init_coins
end
function UserInfo:getIcon()
    return self.icon
end
function UserInfo:getSex()
    return self.sex
end
function UserInfo:getVipLevel()
    return self.vip_level or 0
end
function UserInfo:getIconBorder()
    return self.icon_border
end
function UserInfo:getWinCount()
    return self.win_count
end
function UserInfo:addWinCount()
    self.win_count = self.win_count + 1
end

--总投注数
function UserInfo:getTotalBet()
    return self.total_bet
end
function UserInfo:setTotalBet(total_bet)
    self.total_bet = total_bet
end
function UserInfo:addTotalBet(bet)
    self.total_bet = self.total_bet + bet
end

--当前注数
function UserInfo:getCurBet()
    return self.cur_bet
end
function UserInfo:setCurBet(cur_bet)
    self.cur_bet = cur_bet
end
function UserInfo:addCurBet(bet)
    self.cur_bet = self.cur_bet + bet
end



function UserInfo:getPlayCount()
    return self.play_count
end
function UserInfo:setPlayCount(play_count)
    self.play_count = play_count
end

function UserInfo:getFD()
    --fd 是(file descriptor)，这种一般是BSD Socket的用法，用在Unix/Linux系统上。在Unix/Linux系统下，一个socket句柄
    return self.fd
end
function UserInfo:setFD(fd)
    self.fd = fd
end
--比较fd
function UserInfo:isFD(fd)
    if fd and self.fd==fd then 
        return true
    end
    return false
end

function UserInfo:getClientVersion()
    return self.client_version
end
function UserInfo:getChannel()
    return self.channel
end
function UserInfo:getCurChannel()
    return self.cur_channel
end
function UserInfo:getDeviceId()
    return self.device_id
end
function UserInfo:getDeviceBrand()
    return self.device_brand
end
function UserInfo:getIsRobot()
    return self.is_robot
end
function UserInfo:setIsRobot(is_robot)
    self.is_robot = is_robot
end

--是否机器人
function UserInfo:isRobot()
    return self.is_robot
end

--是否真人
function UserInfo:isPlayer()
    return (not self.is_robot)
end

--客户端需要返回的个人信息
function UserInfo:getClientInfo()
    return {
        uid = self.uid,
        name = self.name,
        coins = self.coins,
        icon = self.icon,
        seat_id = self.seat_id,
        sex = self.sex,
        vip_level = self.vip_level,
        icon_border = self.icon_border,

        win_count = self:getLatelyWinNum(), --最近20局
        total_bet = self.total_bet or 0,
        play_count = #self.lately20, --最近20局
    }
end

--最后发送聊天的时间
function UserInfo:getLoasChatTime()
    return self.last_chat_time
end
function UserInfo:setLoasChatTime(time)
    self.last_chat_time = time
end

--是否游戏状态中
function UserInfo:getPlaying()
    return self.playing
end
function UserInfo:setPlaying(playing)
    self.playing = playing
end

--是否离线状态中
function UserInfo:getDisconnect()
    return self.disconnect
end
function UserInfo:setDisconnect(disconnect)
    self.disconnect = disconnect
end

--是否下注过
function UserInfo:getBeted()
    return self.beted
end
function UserInfo:setBeted(beted)
    if not self.beted then
        self.play_count = self.play_count + 1
    end
    self.beted = beted
end

--百家乐的赔率类型
function UserInfo:getRateType()
    return self.rate_type
end
function UserInfo:setRateType(rate_type)
    self.rate_type = rate_type
end

--连续没下注的次数 （超过3次会被踢出桌子
function UserInfo:getNoBetCount()
    return self.no_bet_count
end
function UserInfo:setNoBetCount(count)
    self.no_bet_count = count
end

--机器人类型
function UserInfo:getRobotType()
    return self.robot_type
end
function UserInfo:setRobotType(robot_type)
    self.robot_type = robot_type
end

--机器人类型id
function UserInfo:getRobotTypeId()
    return self.robot_type_id
end
function UserInfo:setRobotTypeId(robot_type_id)
    self.robot_type_id = robot_type_id
end

--可下注列表
function UserInfo:getCanBetList()
    return self.can_bet_lit
end
function UserInfo:setCanBetList(can_bet_lit)
    self.can_bet_lit = can_bet_lit
end

--最近20局输赢
function UserInfo:getLatelyWinNum()
    local num = 0
    for k, v in pairs(self.lately20) do 
        if v then 
            num = num + 1
        end
    end
    return num
end
function UserInfo:setLatelyWin(win)
    local index = #self.lately20 + 1
    if index > 20 then 
        table_remove(self.lately20, 1)        
    end
    table_insert(self.lately20, win)
end

--最近20局赢率(xx%)
function UserInfo:getLatelyWinRate()
    if #self.lately20 == 0 then 
        return 0
    end
    return math_floor(self:getLatelyWinNum()/#self.lately20 * 100)
end

--比较是否一样
function UserInfo:compare(user)
    if not user then
        return false
    end
    if self.uid == user:getUid() then 
        return true
    end
    return false
end

function UserInfo:add_qhb_record(uid,coins,num,coin,win_lose,is_lei)
    --dbglog("add_qhb_record:",uid,coins,num,coin,win_lose,is_hit_lei)
    table_insert(self.qhb_record,{is_lei = is_lei})
    --table_insert(self.qhb_record,{coins = coins,num = num,win_lose = win_lose,is_lei = is_lei})
    if #self.qhb_record > 20 then
        table_remove(self.qhb_record,1)
    end
    dbglog(tostring_r(self.qhb_record))
end

function UserInfo:add_fhb_record(uid,coins,hit_player_count,win_lose)
   -- dbglog("add_fhb_record:",uid,coins,hit_player_count,win_lose)
    table_insert(self.fhb_record,{coins = coins})
    -- table_insert(self.fhb_record,{coins = coins,hit_count = hit_player_count,win_lose = win_lose})   
    if #self.fhb_record > 20 then
        table_remove(self.fhb_record,1)
    end
end

function UserInfo:getFhbRecord(uid)
    return self.fhb_record
end

function UserInfo:getQhbRecord(uid)
    return self.qhb_record
end

--进入桌子时身上的金币
function UserInfo:getEnterCoins()
    return self.enter_coins
end
function UserInfo:setEnterCoins(coins)
    self.enter_coins = coins or self.coins
end

--进入桌子时存款金币
function UserInfo:getEnterSafeCoins()
    return self.enter_safe_coins
end
function UserInfo:setEnterSafeCoins(coins)
    self.enter_safe_coins = coins or self.safe_box
end

--进入桌子时的时间
function UserInfo:getEnterTime()
    return self.enter_time
end
function UserInfo:setEnterTime(time)
    self.enter_time = time or os.time()
end

--保险箱
function UserInfo:getSafeBox()
    return self.safe_box
end
function UserInfo:setSafeBox(safe_box)
    if not safe_box then 
        return 
    end
    self.safe_box = safe_box    
end

--入桌后的总输赢
function UserInfo:getWinLose()
    return self.win_lose
end
function UserInfo:setWinLose(win_lose)
    self.win_lose = win_lose or 0
end
function UserInfo:addWinLose(win_lose)
    self.win_lose = self.win_lose + win_lose    
end

--入桌后的总服务费
function UserInfo:getFree()
    return self.free
end
function UserInfo:setFree(free)
    self.free = free or 0
end
function UserInfo:addFree(free)
    self.free = self.free + free    
end

--手机系统
function UserInfo:getOsSystem()
    return self.os_system
end

--平台
function UserInfo:getPlatform()
    return self.platform 
end

--入桌后的总产出
function UserInfo:getProduce()
    return self.produce
end
function UserInfo:setProduce(produce)
   self.produce = produce or 0
end
function UserInfo:addProduce(produce)
    self.produce = self.produce + produce    
end

















return UserInfo